package com.zxy.car.controller;

import java.util.Arrays;
import java.util.Map;

import com.zxy.car.config.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zxy.car.entity.AdminEntity;
import com.zxy.car.service.AdminService;
import com.zxy.common.utils.PageUtils;
import com.zxy.common.utils.R;



/**
 * 
 *
 * @author zxy
 * @email 1695483548@qq.com
 * @date 2022-04-15 10:01:02
 */
@RestController
@RequestMapping("car/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = adminService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{name}")
    public R info(@PathVariable("name") String name){
		AdminEntity admin = adminService.getById(name);

        return R.ok().put("admin", admin);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AdminEntity admin){
		adminService.save(admin);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AdminEntity admin){
		adminService.updateById(admin);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] names){
		adminService.removeByIds(Arrays.asList(names));

        return R.ok();
    }

    /*
     * 登录
     * */
    @RequestMapping("/userlogin")
    public R userlogin(@RequestBody AdminEntity admin){
        System.out.println(jwtUtil);
        AdminEntity adminEntity = adminService.getById(admin.getName());
        if (adminEntity!=null){
            if (adminEntity.getPwd().equals(admin.getPwd())){
                if (adminEntity.getStatus().equals(0)){
                    String token = jwtUtil.createJWT(admin.getName(),admin.getName(),"admin");
                    return R.ok().put("token",token).put("loginName",adminEntity.getName());
                }
            }
        }
        return R.error(500,"error~~~~~~~~~~~");
    }
    @RequestMapping("/info")
    public R info(@RequestBody AdminEntity admin){
        System.out.println(jwtUtil);
        AdminEntity adminEntity = adminService.getById(admin.getName());
        if (adminEntity!=null){
            if (adminEntity.getPwd().equals(admin.getPwd())){
                if (adminEntity.getStatus().equals(1)){
                    String token = jwtUtil.createJWT(admin.getName(),admin.getName(),"admin");
                    return R.ok().put("token",token).put("loginName",adminEntity.getName());
                }
            }
        }
        return R.error(500,"error~~~~~~~~~~~");
    }

}
